Flash Article

Creating Movie Clip Subclasses in ActionScript 2.0


Table of Contents

  1. Introduction
  2. Classes and Objects, Symbols and Clips
  3. A Short History of Movie Clip Behaviors
  4. Tutorial 1: Using a Movie Clip Subclass to Make a Bouncing Ball Animation
  5. Tutorial 2: Creating a More Extensive Grab-and-Match Puzzle

A Short History of Movie Clip Behaviors

Macromedia Flash 5 was a wonderful step forward for people who wanted to write ActionScript to control movie clips in a Flash movie. Although it was possible to write complex ActionScript event handlers for individual movie clips, it was cumbersome to create a movie clip symbol in the library that you could just drop on the Stage with predefined behaviors. For example, to make a movie clip do something before the next frame appears, an onClipEvent handler can be attached directly to the clip on the Stage:

onClipEvent(enterFrame){
   _x += 10; // Move the clip ten pixels to the right between each frame.
}

In Macromedia Flash 5 you had to nest movie clips inside other movie clips to give them predefined behaviors. For example, if you wanted to drag a movie clip of a ball onto the Stage that was already preprogrammed to bounce around the Stage, the ball clip's symbol in the library had to contain another clip (often referred to as a controller or process clip) with the necessary bouncing behaviors attached to it. While essential in Flash 5, these clips littered the library and distributed the movie's code in often hard-to-find places.

Macromedia Flash MX removed this need for embedding clips within movie clips. As an alternative, it made it possible to create types of ActionScript objects (classes) that were subclasses (or derived from) the MovieClip object. ActionScript objects that were subclasses of the MovieClip object could define method handlers for movie clip events, such as entering a frame or being loaded prior to becoming visible for the first time (onEnterFrame and onLoad methods), and could manipulate movie clip properties, such as _alpha, as though they were regular object properties. Once a movie clip subclass is defined in ActionScript, it can be associated with a movie clip symbol in the library. A big advantage to this approach—aside from getting rid of unnecessary inner clips—is that the movie clip code could be found in one of a few easy-to-check locations:

  • The first frame of the main Timeline of a movie
  • The main Timeline of the movie clip
  • An external ActionScript file included in the published movie

The basic process in Macromedia Flash MX for creating a set of behaviors for a movie clip symbol looked like this: You first create a particular class in ActionScript. You then define how movie clips associated with that class will behave by adding event handlers to the class definition. Finally, you connect your class with a particular movie clip by "registering" the class with the movie clip's linked identifier in the library. Once this process is complete, your class and its event handlers are automatically associated with every instance of the movie clip created afterwards.

Here is a short ActionScript 1.0 code snippet that doesn't do anything more than move the clip 10 pixels to the right between frames:

function Ball(){
}
Ball.prototype = new MovieClip();
Object.registerClass("Ball", Ball);

Ball.prototype.onEnterFrame = function(){
	this._x += 10;
}

The onEnterFrame method of the Ball class provides a custom behavior for the movie clip that you would otherwise have to write into an onMovieClip(enterFrame) handler attached to an actual clip.

ActionScript 2.0 allows you to do the same thing you can do in ActionScript 1.0. However, class definitions are simpler to write. Instead of using the prototype object, you can almost completely dispense with the keyword this and organize your source code in one area: a series of well organized ActionScript 2.0 class files. Here is the previous code snippet rewritten in ActionScript 2.0:

dynamic class Ball extends MovieClip {
   function onEnterFrame() {
      _x += 10;
   }
}